package com.aote.entity.action;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.aote.mvc.event.Action;
import com.aote.util.ExpressionHelper;
import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.user.User;

/**
 * 找对象动作，根据输入的条件给高级简单实体找对象
 */
public class SearchEntityAction extends Action {
	public void performAction(Element config, HttpServletRequest request,
			Map attrs) {
		// 得到要找的对象
		User user = (User) request.getSession().getAttribute("loginUser");
		Map cond = this.getCondition(config, attrs, user);
		String eType = config.getAttribute("entitytype");
		Collection l = EntityManager.getInstance().getEntities(eType, cond,
				this.component.getPage());
		// 如果没有找到，或者找到多个，提示错误
		if (l.size() != 1) {
			this.component.getPage().showError("alert('没有找到对象');");
			return;
		}
		// 把找到的对象给父实体
		String attrName = config.getAttribute("attr");
		Entity entity = (Entity) l.iterator().next();
		AdvancedSimpleEntity model = (AdvancedSimpleEntity) component
				.getModel();
		model.setAssociateEntity(attrName, entity);
		model.dealOneSourceByAttr(attrName, entity);
	}

	/**
	 * 得到条件
	 */
	private Map getCondition(Element config, Map attrs, User user) {
		throw new UnsupportedOperationException();
	}
}
